package com.shf.taobao.service.impl;


import com.shf.film.common.PageInfo;
import com.shf.film.common.PageSearchCondition;
import com.shf.film.exception.ServiceException;
import com.shf.taobao.TradeService;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TradesBoughtGetRequest;
import com.taobao.api.response.TradesBoughtGetResponse;

public class TradeServiceImpl implements TradeService{

	@Override
	public PageInfo getTrades(PageSearchCondition condition) throws ServiceException, ApiException {
		PageInfo page = condition.getPage();
		TaobaoClient client=new DefaultTaobaoClient(ServiceImpl.url, ServiceImpl.appKey, ServiceImpl.secret);
		TradesBoughtGetRequest req=new TradesBoughtGetRequest();
		req.setPageNo(new Long(page.getPageIndex()));
		req.setPageSize(new Long(page.getPageSize()));
		req.setFields("seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee,adjust_fee,post_fee,total_fee,pay_time,end_time,modified,consign_time,buyer_obtain_point_fee,point_fee,real_point_fee,received_payment,commission_fee,pic_path,num_iid,num_iid,num,price,cod_fee,cod_status,shipping_type,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders.title,orders.pic_path,orders.price,orders.num,orders.iid,orders.num_iid,orders.sku_id,orders.refund_status,orders.status,orders.oid,orders.total_fee,orders.payment,orders.discount_fee,orders.adjust_fee,orders.sku_properties_name,orders.item_meal_name,orders.buyer_rate,orders.seller_rate,orders.outer_iid,orders.outer_sku_id,orders.refund_id,orders.seller_type");
		TradesBoughtGetResponse response = client.execute(req , ServiceImpl.SESSIONKEY.get());
		page.setData(response.getTrades());
		page.setTotalRec(Integer.parseInt(response.getTotalResults().toString()));
		int totalPage = (page.getTotalRec() % page.getPageSize() == 0) ? (page.getTotalRec() / page
				.getPageSize())
				: (page.getTotalRec() / page.getPageSize()) + 1;
		page.setTotalPage(totalPage);
		return page;
	}

}
